import logging

from requests.structures import CaseInsensitiveDict


class HttpResponse:
    url: str
    method: str
    data: dict
    content: str
    request_name: str
    stat: bool
    req_data: any
    status: int
    headers: CaseInsensitiveDict[str]

    def __init__(self, request_name: str, url: str, method: str, req_data=None, data=None, content=None, status=None,
                 headers: CaseInsensitiveDict[str] = CaseInsensitiveDict(), stat=False):
        self.headers = headers
        self.url = url
        self.req_data = req_data
        self.request_name = request_name
        self.method = method
        self.status = status
        self.content = content
        self.data = data
        self.stat = stat

    def content_type(self) -> str:
        if self.headers.__contains__("Content-Type"):
            return self.headers["Content-Type"]
        return ""

    def assertion(self):
        if self.stat is True:
            return self
        raise Exception(self.log_str())

    def log_str(self):
        t = self.content_type()
        if type(self.content) is str:
            return (
                f"{self.request_name}  request {'success' if self.stat else 'failure'} [{self.method}] >>> {self.url} . "
                f"content-type = {t} , status_code = {self.status} , req_data = {self.req_data} ,"
                f" msg >> {self.content}")
        else:
            return (
                f"{self.request_name}  request {'success' if self.stat else 'failure'} [{self.method}] >>> {self.url} . "
                f"content-type = {t} , status_code = {self.status} , req_data = {self.req_data}")

    def print_log(self):
        if self.stat is True:
            logging.info(self.log_str())
        else:
            logging.error(self.log_str())

    def is_success(self) -> bool:
        if self.stat is True:
            return True
        else:
            logging.error(self.log_str())
            return False

    def is_failure(self) -> bool:
        return not self.is_success()
